<?php

class Controller
{

    // Private vars
    protected $model;
    protected $view;
    protected $modules;
    protected $conf;

    public function __construct()
    {

    }

    public function linkModel(&$model)
    {
        $this->model = $model;
    }

    public function linkView(&$view)
    {
        $this->view = $view;
    }

    public function linkModules(&$modules)
    {
        $this->modules = $modules;
    }

    public function setConf(&$conf)
    {
        $this->conf = $conf;
    }

    // Build link
    public function link($modName, $actName = false)
    {
        if (!empty($actName))
        {
            return '/'.$modName.'/'.$actName.'/';
        }
        else
        {
            return '/'.$modName.'/';
        }
    }

    // Get value from post or GET
    public function getValue($value)
    {
        if (isset($_POST[$value]))
        {
            return $this->sanitizeValue($_POST[$value]);
        }
        else if (isset($_GET[$value]))
        {
            return $this->sanitizeValue($_GET[$value]);
        }
        else
        {
            return false;
        }
    }

    // Sanitize value
    private function sanitizeValue($val)
    {
        if (is_array($val))
        {
            foreach ($val as $key => $value)
            {
                $value = $this->sanitizeValue($value);
                $val[$key] = $value;
            }
            return $val;
        }
        else
        {
            $val = htmlspecialchars($val , ENT_COMPAT, 'ISO-8859-1');
            return $val;
        }
    }

    // Append notice to session to show when loading the page
    public function notice($msg)
    {
        if ($msg != '')
        {
            $this->model->notice($msg);
        }
    }

    // Append error to session to show when loading the page
    public function error($msg)
    {
        if ($msg != '')
        {
            $this->model->error($msg);
        }
    }

    // Get notices
    public function getNotices()
    {
        return $this->model->getNotices();
    }

    // Redirect to
    public function redirectTo($modName, $actName = false, $params = false)
    {
        $url = $this->link($modName, $actName);
        if (!empty($params))
        {
            $url.=$params;
        }
        header("Location: $url");
        die();
    }

    // Redirect to home if not logged
    public function userLoggedIn($onlyDie = false)
    {
        if ($this->user->isLoggedIn())
        {
            if ($this->user->hasPendAuth())
            {
                $this->redirectTo(MODULE_LOGIN, MODULE_LOGIN_ACTION_TOKEN);
            }
            else
            {
                return true;
            }
        }
        else
        {
            if ($onlyDie)
            {
                die();
            }
            else
            {
                $this->redirectTo(MODULE_LOGIN);
            }
        }
    }

    // Redirect to home if not normal
    public function isNormal($onlyDie = false)
    {
        if ($this->user->isNormal())
        {
            return true;
        }
        else
        {
            if ($onlyDie)
            {
                die();
            }
            else
            {
                $this->redirectTo(MODULE_LOGIN);
            }
        }
    }

    // Redirect to home if not admin
    public function isAdmin($onlyDie = false)
    {
        if ($this->user->isAdmin())
        {
            return true;
        }
        else
        {
            if ($onlyDie)
            {
                die();
            }
            else
            {
                $this->redirectTo(MODULE_LOGIN);
            }
        }
    }

    // Redirect to home if not master
    public function isMaster($onlyDie = false)
    {
        if ($this->user->isMaster())
        {
            return true;
        }
        else
        {
            if ($onlyDie)
            {
                die();
            }
            else
            {
                $this->redirectTo(MODULE_LOGIN);
            }
        }
    }

    // Redirect to home if not security
    public function isSecurity($onlyDie = false)
    {
        if ($this->user->isSecurity())
        {
            return true;
        }
        else
        {
            if ($onlyDie)
            {
                die();
            }
            else
            {
                $this->redirectTo(MODULE_LOGIN);
            }
        }
    }

    // Get page (used for pagination)
    public function getPage()
    {
        $page = $this->getValue('page');
        $page = intval($page);
        if (empty($page)) $page = 1;
        return $page;
    }

    // Print array to Json
    public function jSon($array)
    {
        echo json_encode($array);
        die();
    }

}
?>